package com.example.actearly_revised;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.example.actearly_revised.QuestionsObject.Node;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;

import android.os.Bundle;

import android.util.Log;

import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;


@TargetApi(16)
public class HelpfulHints extends Activity {
	@SuppressLint({ "NewApi", "NewApi" })
	View imageButton;
	TextView textView1;
	Button nextButton;
	Button backButton;
	String ageGroups[];
	Spinner urgentAgeSpinner;
	SQLogic conn;
	static int current =0;
	Map<Integer, List<String>> helpfulHints = new HashMap<Integer, List<String>>();
	ArrayAdapter<String> urgentAdapter;
	int[] ageID;
	static int milestone;
	int count = 0;
	public static HashMap<Integer, Integer> holdstatus = new HashMap<Integer, Integer>();
	int CurrentAgeIndex;
	ProgressBar percentDone;


	@SuppressLint({ "NewApi", "NewApi" })
	@TargetApi(16)
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		for(int i = 0; i < ageID.length; i++){
			holdstatus.put(ageID[i], 0);
		}
		milestone = QuicklistTabHomePage.currentmilestone;
		CurrentAgeIndex = indexOf(milestone,ageID);
		//Helpful_Hints_Methods hhm = new Helpful_Hints_Methods(HelpfulHints.this);

		conn = new SQLogic(HelpfulHints.this);
		//SQLogic conn = new SQLogic(HelpfulHints.this);
		conn.open();
		HintObject hintob = conn.RetrieveHelpfulHints(milestone);
		conn.close();
		helpfulHints.put(milestone, new LinkedList<String>());

		//create progress bar
				percentDone= (ProgressBar) findViewById(R.id.hintsProgress);
				// reset the progress bar to the default value of 0
				percentDone.setProgress(0);
				//set the maximum value of progress bar based on # of questions
				percentDone.setMax(hintob.count()-1);
				ageGroups= getResources().getStringArray(R.array.milestoneAges);




		Iterator<Integer> newIterator = hintob.getMap().keySet().iterator();
		while(newIterator.hasNext()){
			//Iterator<String> temp = newIterator;
			Integer temp1 =newIterator.next();
			helpfulHints.get(milestone).add(hintob.getMap().get(temp1).Hint);

			//System.out.println("GGGGGGGGGGGGGGGGGG" +helpfulHints.get(ageID[index]));
			//count++;
		}
		//conn.open();
		//conn.close();
		try{
			/*Intent intent = new Intent(this, NotificationReceiverActivity.class);

		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

		// Build notification
		// Actions are just fake
		Notification noti = new Notification.Builder(this)
		.setContentTitle("Make sure you see your \n Doctor on a regular basis!")
		.setContentText("Act Early").setSmallIcon(R.drawable.launchericon)
		.setContentIntent(pIntent).build();


		NotificationManager notificationManager =  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		// Hide the notification after its selected
		noti.flags |= Notification.FLAG_AUTO_CANCEL;

		notificationManager.notify(0, noti);*/
			//setContentView(R.layout.activity_how_to_help_page);

		}
		catch(Exception e){
			e.printStackTrace();
		}

		setContentView(R.layout.activity_helpful_hints);
		Log.d("1", "1");
		//LinearLayout pageLayout=(LinearLayout)findViewById(R.id.carouselLayout);
		//pageLayout.setBackgroundResource(R.drawable.background);
		imageButton = findViewById(R.id.imageButton1);
		Log.d("1", "2");

		textView1 = (TextView) findViewById(R.id.textView1);
		Log.d("1", "3");

		//font change
		Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");

		textView1.setText(helpfulHints.get(milestone).get(current));
		backButton = (Button) findViewById(R.id.back2);
		nextButton = (Button) findViewById(R.id.next1);

		textView1.setTypeface(customFont);
		backButton.setTypeface(customFont);
		nextButton.setTypeface(customFont);
		String[] items= getResources().getStringArray(R.array.milestoneAges);
		urgentAgeSpinner = (Spinner) findViewById(R.id.urgentlistSpinner);
		ArrayAdapter<String> urgentlistAdapter = new ArrayAdapter<String>(this,
				R.layout.activity_spinner_layout, items) {

			public View getView(int position, View convertView, ViewGroup parent) {
				View v = super.getView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);
				//((TextView) v).setTextColor(-1);


				return v;
			}

			public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
				View v =super.getDropDownView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);

				return v;
			}
		};

		urgentlistAdapter.setDropDownViewResource(R.layout.activity_spinner_layout);
		urgentAgeSpinner.setAdapter(urgentlistAdapter);

		urgentAgeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				current =0;
				int index = arg0.getSelectedItemPosition();
				// storing string resources into Array
				String name[] = getResources().getStringArray(R.array.milestoneAges);
				Log.d("switch","before switch");
				SQLogic conn = new SQLogic(HelpfulHints.this);
				conn.open();
				HintObject hintob = conn.RetrieveHelpfulHints(ageID[index]);
				conn.close();
				helpfulHints.put(ageID[index], new LinkedList<String>());
				Iterator<Integer> newIterator = hintob.getMap().keySet().iterator();
				while(newIterator.hasNext()){
					//Iterator<String> temp = newIterator;
					Integer temp1 =newIterator.next();
					helpfulHints.get(ageID[index]).add(hintob.getMap().get(temp1).Hint);

					//System.out.println("GGGGGGGGGGGGGGGGGG" +helpfulHints.get(ageID[index]));
					count++;
				}
				milestone = ageID[index];
				current = holdstatus.get(milestone);
				textView1.setText(helpfulHints.get(milestone).get(current));
				//question= new String[db.count()];

				//ListView lv= (ListView) findViewById(android.R.id.list);
				//lv.setAdapter(new CustomListAdapter(UrgentlistPage.this,question));				

				QuicklistTabHomePage.currentmilestone = milestone;

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// do nothing
			}

		});     


		backButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				count--;
				if(current > 0){
					current--;
				}
				else{
					current = helpfulHints.get(milestone).size()-1;
				}
				go();
				// TODO Auto-generated method stub

			}
		});
		nextButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				count++;
				if(current < helpfulHints.get(milestone).size()-2){
					current++;
				}
				else{
					current =0;
				}
				go();
			}
		});

		imageButton.setOnClickListener(new View.OnClickListener() { // clicked yes

			public void onClick(View v) {
				//imageButton.set
				Log.d("1", "4");
				//count++;

				//LinearLayout pageLayout=(LinearLayout)findViewById(R.id.carouselLayout);
				//pageLayout.setBackgroundResource(R.drawable.background);
			}
		});


		imageButton.setOnTouchListener(new OnSwipeTouchListener() {
			public void onSwipeTop() {
				System.out.println("T");
				// Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
			}
			public void onSwipeRight() {
				System.out.println("R");
				current++;
				count++;
				go();
				// Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
			}
			public void onSwipeLeft() {
				System.out.println("L");
				current--;
				count--;
				go();
				//  Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
			}
			public void onSwipeBottom() {
				System.out.println("B");
				// Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
			}

		});

	}

	@Override
	public void onResume(){
		super.onResume();
		milestone = QuicklistTabHomePage.currentmilestone;
		CurrentAgeIndex = indexOf(milestone,ageID);
		urgentAgeSpinner.setSelection(CurrentAgeIndex);
	}

	public void go(){
		//System.out.println(helpfulHints.get(milestone));
		textView1.setText(helpfulHints.get(milestone).get(current));
		int temp =  current;
		holdstatus.put(milestone, temp);
		if(count == 0){
			((ImageView) imageButton).setImageResource(R.drawable.image1);			
			//textView1.setText("This is the first Helpful hint");
		}
		if(count == 1){
			((ImageView) imageButton).setImageResource(R.drawable.image2);
			//textView1.setText("This is the second Helpful hint");
		}
		else if(count == 2){
			((ImageView) imageButton).setImageResource(R.drawable.image3);
			//textView1.setText("This is the third Helpful hint");
			count =-1;
		}
		else if(count > 2){
			count =0;
		}
		else if(count < 0){
			count=2;
		}

	}

	public int indexOf(int target,int[] list){
		int result = -1;
		for(int i = 0; i < list.length; i++){
			if(list[i] == target){
				result = i;
				break;
			}
			result = -1;
		}
		return result;
	}
}

/*
		final CarouselLayout layout = (CarouselLayout) findViewById(R.id.carouselLayout);
		final CarouselAdapter adapter = new CarouselAdapter();

		layout.setAdapter(adapter);

		//Variant 2
		layout.setTranslate(50);
		layout.setSpaceBetweenViews(100);

		//Variant 3
		layout.setTranslate(50);
		layout.setSpaceBetweenViews(100);
		layout.setRotation(-10);

		//Variant 4
		layout.setTranslate(-50);
		layout.setSpaceBetweenViews(100);
		layout.setRotation(-10);
	}

	//~--- inner classes ------------------------------------------------------

	private class CarouselAdapter extends BaseAdapter {
		private int[] mImagesID = {
				R.drawable.image1, 
				R.drawable.image2, 
				R.drawable.image3
		};

		public int getCount() {
			return mImagesID.length;
		}

		public Object getItem(int position) {
			return mImagesID[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if ((position < 0) && (position > (getCount() - 1))) {
				Log.e("UITEST", "Unexistent position requested");

				return null;
			}

			ImageView imageView;
			//TextView textView = null;
			if (convertView != null) {
				imageView = (ImageView) convertView;

			} else {            	
				imageView = new ImageView(getApplicationContext());
				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				//Layout params must be FILL_PARENT
				imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				imageView.setAlpha(200);
			}
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.toast_layout,
					(ViewGroup) findViewById(R.id.toast_layout_root));
			imageView.setImageResource(mImagesID[position]);
			//textView.setText("LoL");
			Context context = getApplicationContext();
			Toast toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();
			return imageView;
		}
	}
}*/
